Imports DotNetPM.Business.Questionnaire

Partial Class Modules_Questionnaires_Controls_logMultipleChoicesMultipleAnswers
    Inherits System.Web.UI.UserControl
    Implements UserControls.ILogicUC

    Dim _question As Question

    Public Property Logic() As String Implements DotNetPm.Business.Questionnaire.UserControls.ILogicUC.Logic
        Get
            Dim sb As New StringBuilder
            sb.Append("4|")
            sb.Append(IIf(ddlAnswer.SelectedIndex = 2, ddlOperators2.SelectedIndex, ddlOperators1.SelectedIndex))
            sb.Append("|")
            sb.Append(IIf(ddlAnswer.SelectedIndex = 2, txtChoices.Text, rblChoices.SelectedIndex))
            sb.Append("|")
            sb.Append(ddlAnswer.SelectedValue)
            Return sb.ToString
        End Get
        Set(ByVal value As String)
            Dim val As String() = value.Split("|")
            ddlAnswer.SelectedValue = val(3)
            If ddlAnswer.SelectedIndex = 2 Then
                ddlOperators2.SelectedIndex = val(1)
                txtChoices.Text = val(2)
            Else
                ddlOperators1.SelectedIndex = val(1)
                rblChoices.SelectedIndex = val(2)
            End If
            AdjustControls()
        End Set
    End Property

    Public Property Question() As Question Implements DotNetPm.Business.Questionnaire.UserControls.ILogicUC.Question
        Get
            Return _question
        End Get
        Set(ByVal value As Question)
            _question = value
        End Set
    End Property

    Public Sub Reset() Implements DotNetPm.Business.Questionnaire.UserControls.ILogicUC.Reset
        ddlAnswer.SelectedIndex = -1
        ddlOperators1.SelectedIndex = -1
        ddlOperators2.SelectedIndex = -1
        rblChoices.SelectedIndex = -1
        txtChoices.Text = String.Empty
    End Sub

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        PopulateChoices()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        AdjustControls()
    End Sub

    Private Sub AdjustControls()
        Select Case ddlAnswer.SelectedIndex
            Case 0  '-1
                rblChoices.Visible = False
                reqChoices.Visible = False
                rangeChoices.Visible = False
                txtChoices.Visible = False
                rblChoices.Visible = False
                ddlOperators1.Visible = False
                ddlOperators2.Visible = False
            Case 1  ' 0
                reqChoices.ControlToValidate = "rblChoices"
                rangeChoices.Visible = False
                txtChoices.Visible = False
                rblChoices.Visible = True
                ddlOperators1.Visible = True
                ddlOperators2.Visible = False
            Case 2  ' 1
                reqChoices.ControlToValidate = "txtChoices"
                rangeChoices.Visible = True
                txtChoices.Visible = True
                rblChoices.Visible = False
                ddlOperators1.Visible = False
                ddlOperators2.Visible = True
        End Select
    End Sub

    Private Sub PopulateChoices()
        With rblChoices
            .Visible = True
            .DataSource = QuestionODS.GetChoices(_question.ID)
            .DataTextField = "Choice"
            .DataValueField = "SortID"
            .DataBind()
        End With
        rangeChoices.MaximumValue = rblChoices.Items.Count
    End Sub

End Class
